

clear all
cap log close

log using "${logfiles}t_step_11b.log", replace


/*================================================================

Pull back in data used in Table 2 (i.e. the unbalanced panel of workplace violence) to run power analysis on
		
/=================================================================*/

*Graph style
sysdir set PERSONAL "W:\ado"
set scheme kailaScheme

use "W:\workplace\data\DV_workplace_2000_lag.dta", clear
foreach Y of numlist 2001/2018{
    append using "W:\workplace\data\DV_workplace_`Y'_lag.dta"
} 

// victims
rename victim_shnro shnro
duplicates report shnro year_event
duplicates drop shnro year_event, force 



gen year=year_event-1  
* merge with crime records for each person
merge m:1 shnro year using  "${dataout}crime_records_year.dta"
drop if _merge==2
drop _merge 
replace record=0 if record==.
replace cum_record=0 if cum_record==.
gen share_record=cum_record>0



foreach X in sukup ptoim1 tyotu svatva toimiala ammattikoodi tyokk ututku ika tyrtu svatvp{
rename  victim_lag_`X'	`X'
}


	
	*Employment
	destring ptoim1, replace
	gen employed=ptoim1==11 & victim_sykstun_lag!="."

	*Education variables
	g educ=ututku 
	destring educ, replace
	gen level=substr(ututku,1,1)
	destring level, replace
	replace level=0 if level==. 
	
	
	*** dummy variable for education 
	gen college=level>=6
	gen high_school=leve>=3 & level<=5
	gen ho=level<3

	

	* Earnings
	replace tyotu = 0 if missing(tyotu)
	replace tyrtu = 0 if missing(tyrtu)
	


	* Sum labor and enrepreneur earnings 
	gen allEarnings =  tyotu + tyrtu
	gen defendant_allEarnings=defendant_tyotu + defendant_tyrtu
 
	
	
	gen age=ika
	gen defendant_age=defendant_ika
	gen gender=(sukup=="1") 
	
	*Deflate the earnings 
	fmerge m:1 year using  "${dataout}\cpi"
	keep if _merge == 3 
	drop _merge
	

	gen realAllEarn = allEarnings/cpi
	gen defendant_realAllEarn = defendant_allEarnings/cpi

	
	drop cpi svatva 



	*Occupation codes
	gen occ=real(substr(ammattikoodi, 1,1))
	gen manager=(occ==1)
	
	gen victim=1 
	gen mm=(suspect_sex=="1" & plaintiff_sex=="1")
	gen mf=(suspect_sex=="1" & plaintiff_sex=="2")
	
save "W:\workplace\data\DV_workplace_victim_2000_2018_lag_unbalanced.dta", replace 





// defendants
use "W:\workplace\data\DV_workplace_2000_lag.dta", clear
foreach Y of numlist 2001/2018{
    append using "W:\workplace\data\DV_workplace_`Y'_lag.dta"
} 



rename defendant_shnro shnro
duplicates report shnro year_event
duplicates drop shnro year_event, force 



gen year=year_event-1  
* merge with crime records for each person
merge m:1 shnro year using  "${dataout}crime_records_year.dta"
drop if _merge==2
drop _merge 
replace record=0 if record==.
replace cum_record=0 if cum_record==.
gen share_record=cum_record>0


foreach X in sukup ptoim1 tyotu svatva toimiala ammattikoodi tyokk ututku ika tyrtu svatvp{
rename  defendant_lag_`X'	`X'
}


	
	*Employment
	destring ptoim1, replace
	gen employed=ptoim1==11 & defendant_sykstun_lag!="."

	*Education variables
	g educ=ututku 
	destring educ, replace
	gen level=substr(ututku, 1,1)
	destring level, replace
	replace level=0 if level==. 
	
	
	*** dummy variable for education 
	gen college=level>=6
	gen high_school=leve>=3 & level<=5
	gen ho=level<3

	

	* Earnings
	replace tyotu = 0 if missing(tyotu)
	replace tyrtu = 0 if missing(tyrtu)
	


	* Sum labor and enrepreneur earnings 
	gen allEarnings =  tyotu + tyrtu
	
	
	gen age=ika
	gen gender=(sukup=="1") 
	
	*Deflate the earnings 
	fmerge m:1 year using  "${dataout}\cpi"
	keep if _merge == 3 
	drop _merge
	

	gen realAllEarn = allEarnings/cpi

	
	drop cpi svatva 



	*Occupation codes
	gen occ=real(substr(ammattikoodi, 1,1))
	gen manager=(occ==1)
	
	gen defendant =1 
	
	gen mm=(suspect_sex=="1" & plaintiff_sex=="1")
	gen mf=(suspect_sex=="1" & plaintiff_sex=="2")
	
	save "W:\workplace\data\DV_workplace_defendant_2000_2018_lag_unbalanced.dta", replace 
	
	
	append using "W:\workplace\data\DV_workplace_victim_2000_2018_lag_unbalanced.dta"
	
	save "W:\workplace\data\DV_workplace_defendantvictim_2000_2018_lag_unbalanced.dta", replace

keep if defendant_sykstun_lag== victim_sykstun_lag & victim_sykstun_lag!=""
gen Earn_positive=realAllEarn
replace  Earn_positive=. if realAllEarn==0

gen time=-1

gen new_wp_crime=victim_sykstun_lag==defendant_sykstun_lag & defendant_sykstun_lag!=""
bys year: ereplace new_wp_crime=max(new_wp_crime)
keep if new_wp_crime==1 
save "W:\workplace\data\DV_workplace_defendantvictim_2000_2018_lag_unbalanced.dta", replace


use "${dataout}total_plant_employee_allyears", replace
drop if sykstun==""

save "${dataout}total_plant_employee_allyears_withfirm", replace


use "W:\workplace\data\DV_workplace_defendantvictim_2000_2018_lag_unbalanced.dta", replace 	

gen sykstun=defendant_sykstun_lag if defendant_sykstun_lag==victim_sykstun_lag

gen temp=1 
collapse temp, by(sykstun year) 
drop temp 

*** merge with all employees in the plant

*gen year=year_event 
merge 1:m sykstun year using "${dataout}total_plant_employee_allyears_withfirm", keepusing(shnro sukup tyotu)
drop if _merge==2 
drop _merge 



bysort sykstun year sukup: egen earnp10_withingender=pctile(tyotu), p(10)
bysort sykstun year: egen earnp10=pctile(tyotu), p(10)
bysort sykstun year sukup: gen earnp10_men=earnp10_withingender if sukup=="1"
bysort sykstun year sukup: gen earnp10_women=earnp10_withingender if sukup=="2"

bysort sykstun year sukup: egen earnp20_withingender=pctile(tyotu), p(20)
bysort sykstun year: egen earnp20=pctile(tyotu), p(20)
bysort sykstun year sukup: gen earnp20_men=earnp20_withingender if sukup=="1"
bysort sykstun year sukup: gen earnp20_women=earnp20_withingender if sukup=="2"


bysort sykstun year sukup: egen earnp33_withingender=pctile(tyotu), p(33)
bysort sykstun year: egen earnp33=pctile(tyotu), p(33)
bysort sykstun year sukup: gen earnp33_men=earnp33_withingender if sukup=="1"
bysort sykstun year sukup: gen earnp33_women=earnp33_withingender if sukup=="2"


bysort sykstun year sukup: egen earnp50_withingender=pctile(tyotu), p(50)
bysort sykstun year: egen earnp50=pctile(tyotu), p(50)
bysort sykstun year sukup: gen earnp50_men=earnp50_withingender if sukup=="1"
bysort sykstun year sukup: gen earnp50_women=earnp50_withingender if sukup=="2"

bysort sykstun year sukup: egen earnp80_withingender=pctile(tyotu), p(80)
bysort sykstun year: egen earnp80=pctile(tyotu), p(80)
bysort sykstun year sukup: gen earnp80_men=earnp80_withingender if sukup=="1"
bysort sykstun year sukup: gen earnp80_women=earnp80_withingender if sukup=="2"


preserve
duplicates drop sykstun year sukup tyotu, force
bys sykstun year sukup: egen earnrank_gend0=rank(tyotu)
bys sykstun year sukup: egen totalrank=count(tyotu)
gen earnrank_gend=earnrank_gend0/totalrank
drop totalrank earnrank_gend0
rename tyotu prior_firmearnings1


save "W:\workplace\data\violent_firm_income_rank_def_gender.dta", replace 
restore

preserve
duplicates drop sykstun year tyotu, force
bys sykstun year: egen earnrank0=rank(tyotu)
bys sykstun year : egen totalrank=count(tyotu)
gen earnrank=earnrank0/totalrank

gen total_employees=totalrank
drop totalrank earnrank0
rename tyotu prior_firmearnings1


save "W:\workplace\data\violent_firm_income_rank_def.dta", replace 
restore


use "${dataout}DV_workplace_defendantvictim_2000_2018_lag_unbalanced", clear
	

sort year time shnro 


keep if mf==1 | mm==1
keep if wp_crime==1 
keep if time==-1


gen prior_firmearnings1=tyotu if defendant==1
rename sukup sukup_main
rename defendant_sykstun_lag sykstun
merge m:1 prior_firmearnings1 sykstun year using  "${dataout}violent_firm_income_rank_def"
drop if _merge==2
drop _merge
drop sukup
gen sukup=sukup_main
replace sukup="" if victim==1
merge m:1 prior_firmearnings1 sykstun year sukup using  "${dataout}violent_firm_income_rank_def_gender"
drop if _merge==2
drop _merge
rename sykstun defendant_sykstun_lag
drop sukup

rename victim_sykstun_lag sykstun 
drop prior_firmearnings1
gen prior_firmearnings1=tyotu if victim==1
merge m:1 prior_firmearnings1 sykstun year using  "${dataout}violent_firm_income_rank_def"
drop if _merge==2
drop _merge
drop sukup
gen sukup=sukup_main
replace sukup="" if defendant==1
merge m:1 prior_firmearnings1 sykstun year sukup using  "${dataout}violent_firm_income_rank_def_gender"
drop if _merge==2
drop _merge
drop sukup

gen baseyr=year


gen treat= wp_crime==1

gen age_time=age+time

drop if treat==0

gen assault =(crimecode6=="210501")
replace assault=1-assault

gen passault =(crimecode6=="210701")
replace passault=1-passault

gen negligent =(crimecode6=="211001")
replace negligent=1-negligent

gen menace =(crimecode6=="250701")
replace menace=1-menace


gen treatPost= treat==1 & time==-1
gen iteraction_occ=treatPost*manager
gen iteraction_assault=treatPost*(assault)


* Generate treatment wave indentifier for each individual
egen baseid=group(shnro baseyr)


/*================================================================

		Figure 6 in the paper 
		
/=================================================================*/

global fe_full = "year" 
global cluster = "shnro"


		 
   * Main regression will all fixed effects
    destring(plaintiff_sex), replace
	destring(suspect_sex), replace
    replace plaintiff_sex=plaintiff_sex-1
	replace suspect_sex=suspect_sex-1

	gen bottom50=0 if tyotu>earnp50 & tyotu!=0  & tyotu!=.
	replace bottom50=1 if tyotu<earnp50 & tyotu!=0  & tyotu!=.
	gen bottom33=0 if tyotu>earnp33 & tyotu!=0  & tyotu!=.
	replace bottom33=1 if tyotu<earnp33 & tyotu!=0  & tyotu!=.
	gen bottom20=0 if tyotu>earnp20 & tyotu!=0  & tyotu!=.
	replace bottom20=1 if tyotu<earnp20 & tyotu!=0  & tyotu!=.
	gen bottom10=0 if tyotu>earnp10 & tyotu!=0  & tyotu!=.
	replace bottom10=1 if tyotu<earnp10 & tyotu!=0  & tyotu!=.
	gen top20=1 if tyotu>earnp80 & tyotu!=0  & tyotu!=.
	replace top20=0 if tyotu<earnp80 & tyotu!=0  & tyotu!=.
	
	gen bottom50_gend=0 if tyotu>earnp50_withingender  & tyotu!=0  & tyotu!=.
	gen bottom33_gend=0 if tyotu>earnp33_withingender  & tyotu!=0  & tyotu!=.
	gen bottom20_gend=0 if tyotu>earnp20_withingender  & tyotu!=0  & tyotu!=.
	gen bottom10_gend=0 if tyotu>earnp10_withingender  & tyotu!=0  & tyotu!=.
	replace bottom50_gend=1 if tyotu<earnp50_withingender  & tyotu!=0  & tyotu!=.
	replace bottom33_gend=1 if tyotu<earnp33_withingender  & tyotu!=0  & tyotu!=.
	replace bottom20_gend=1 if tyotu<earnp20_withingender & tyotu!=0  & tyotu!=.
	replace bottom10_gend=1 if tyotu<earnp10_withingender & tyotu!=0  & tyotu!=.


    reghdfe manager plaintiff_sex if defendant==1, absorb($fe_full) 
	reghdfe manager plaintiff_sex assault passault negligent menace if defendant==1, absorb($fe_full) 
	est sto manager


	reghdfe top20 plaintiff_sex if defendant==1, absorb($fe_full) 
	reghdfe top20 plaintiff_sex assault passault negligent menace if defendant==1, absorb($fe_full) 
	est sto top20
	
	reghdfe bottom20 plaintiff_sex if defendant==1, absorb($fe_full) 
	reghdfe bottom20 plaintiff_sex assault passault negligent menace if defendant==1, absorb($fe_full) 
	est sto bottom20
	
	lab var plaintiff_sex "Measures of Perpetrator Power"
	coefplot manager top20 bottom20, keep(plaintiff_sex) ///
	vertical yline(0) ytitle("Coefficient - Victim Female")  ///
	legend(order(2 "Manager" 4 "Top 20% in Firm" 6 "Bottom 20% in Firm") rows(1)) ///
	name(perp, replace) ///
	recast(bar) barwidth(0.2) fcolor(*0.8) ciopts(recast(rcap)) citop ///
	yscale(r(-0.08 0.14)) ylabel(-0.08(0.02)0.14) 
	graph export "${results}\power_desc_perp.pdf", replace


	
preserve 
set scheme s1mono
	coefplot manager top20 bottom20, keep(plaintiff_sex) ///
	vertical yline(0) ytitle("Coefficient - Victim Female")  ///
	legend(order(2 "Manager" 4 "Top 20% in Firm" 6 "Bottom 20% in Firm") rows(1)) ///
	name(perp, replace) ///
	recast(bar) barwidth(0.2) fcolor(*0.8) ciopts(recast(rcap)) citop ///
	yscale(r(-0.08 0.14)) ylabel(-0.08(0.02)0.14) 
	graph export "${results}\power_desc_perp_bw.pdf", replace
    graph export "${results}\power_desc_perp.eps", replace
restore 
	
log close 
